<?php
namespace App\Http\Controllers\Admin\Eticket;

use App\Http\Controllers\EticketController;
use App\Http\Controllers\WechatController;
use App\Models\Eticket\Activity;
use App\Models\Eticket\Offline;
use App\Models\Eticket\Online;
use App\Models\Eticket\Ticket;
use App\Models\Qrcode;
use App\Service\Eticket\ActivityService;
use Cache;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Service\Eticket\TicketService;
use App\Http\Requests\Eticket\ActivityRequest;

class TicketController extends Controller
{
    const QRCODE = 'qrcode';
    private $ticket;
    private $activity;

    function __construct(ActivityService $activity, TicketService $ticket)
    {
        // 自定义活动中间件
        $this->middleware('check.permission:ticket');
        $this->ticket = $ticket;
        $this->activity = $activity;
    }

    /**
     * 活动列表
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function index($aid)
    {
        $activity = $this->activity->editView($aid);
        return view('admin.eticket.ticket.index')->with(compact('activity'));
    }

    /**
     * datatables获取数据
     * @author 晚黎
     * @date   2016-10-29
     * @return [type]     [description]
     */
    public function ajaxIndex()
    {
        $responseData = $this->activity->ajaxIndex();
        return response()->json($responseData);
    }

    /**
     * 添加活动视图
     * @author 晚黎
     * @date   2016-11-01T16:47:26+0800
     * @return [type]                   [description]
     */
    public function create()
    {
        return view('admin.ticket.create');
    }

    /**
     * 添加活动
     * @author 晚黎
     * @date   2016-11-02T10:30:30+0800
     * @param  TicketRequest $request [description]
     * @return [type]                            [description]
     */
    public function store(ActivityRequest $request)
    {
        $this->activity->storeActivity($request->all());
        return redirect('admin/ticket');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        $activity = $this->activity->editView($id);
        return view('admin.ticket.edit')->with(compact('activity'));
    }

    /**
     * 修改活动视图
     * @author 晚黎
     * @date   2016-11-02T11:42:41+0800
     * @param  [type]                   $id [description]
     * @return [type]                       [description]
     */
    public function edit($id)
    {
        $activity = $this->activity->editView($id);
        return view('admin.ticket.edit')->with(compact('activity'));
    }


    /**
     * 修改活动
     * @author 晚黎
     * @date   2016-11-02T11:58:45+0800
     * @param  TicketRequest $request [description]
     * @param  [type]                   $id      [description]
     * @return [type]                            [description]
     */
    public function update(ActivityRequest $request, $id)
    {
        $this->activity->updateActivity($request->all(), $id);
        return redirect('admin/ticket');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->activity->destroyActivity($id);
        return redirect('admin/ticket');
    }


    /**
     * 统计票种
     * @param $id
     * @param int $way
     * @return \Illuminate\Http\JsonResponse
     */
    public function stat($id, $way = Ticket::ONLINE)
    {
        return response()->json($this->ticket->getTicketsStat($id, $way));
    }

    /**
     * 上传type和文件，批量导入座位号
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchStore(Request $request, $id, $way = Ticket::ONLINE)
    {
        if ($this->ticket->batchStoreTicket($request, $id, $way)) {
            return response()->json(['status' => 0]);
        } else {
            return response()->json(['status' => 1], 500);
        }

    }

    /**
     * 门票列表
     */
    public function all($id)
    {
        $activity = $this->activity->editView($id);
        return view('admin.eticket.ticket.list')->with(compact('activity'));
    }

    /**
     * 门票列表
     */
    public function ajaxList()
    {
        $responseData = $this->ticket->ajaxList();
        return response()->json($responseData);
    }

    /**
     * 异步修改线上线下设置
     * @param $aid
     * @param int $way
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajaxSetting(Request $request, $aid, $way = Ticket::ONLINE)
    {
        $activity = Activity::find($aid);
        if (!$activity) {
            return response()->json(['status' => 1, 'error' => '没有找到该活动'], 404);
        }
        $key = $request->input('key');
        $value = $request->input('value');
        if (in_array($key, ['start_date', 'end_date', 'billing_date'])) {
            $value = strtotime($value);
        } elseif ($key == 'other_school') {
            $value = $value == 'true' ? 1 : 0;
        } else {
            return response()->json(['status' => 2, 'error' => '系统出错，请刷新重试！'], 500);
        }
        if ($way == Ticket::ONLINE) {
            $online = $activity->online;
            if (empty($online)) {
                $online = new Online();
            }
            $online->activity_id = $aid;
            $online->$key = $value;
            $online->save();
        } else {
            $offline = $activity->offline;
            if (empty($offline)) {
                $offline = new Offline();
            }
            $offline->activity_id = $aid;
            $offline->$key = $value;
            $offline->save();
        }
        return response()->json(['status' => 0]);
    }

    /**
     * 返回线下扫码的二维码
     * @param $aid
     * @param $type
     * @param int $way
     * @return array|bool
     */
    public static function getQrcode($aid, $type, $way = Ticket::OFFLINE)
    {
        //TODO delete
//        return [
//            'http://weixin.qq.com/q/02Jim70-v186P1ny6M1p1R',
//            'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFh8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAySmltNzAtdjE4NlAxbnk2TTFwMVIAAgTiuUhZAwQAjScA'
//        ];
        if (empty($type)) {
            return false;
        }
        $key = self::QRCODE . '_' . $aid . '_' . $way . '_' . $type;
        return WechatController::qrcode($key, EticketController::class, 'offlineScan', [$aid, $way, $type]);
    }
}
